import validator from '@/components/rui-auto/validate/rules';

export const formOperation = (params,grid) => {
    params.forEach(e => {
        //设置默认精度
        if (!e.scale) {
            e.scale = 2;
        }
        //设置默认类型 为text
        if (!e.type || e.type == '') {
            e.type = 'text';
        }
        e._type = e.type;
        //设置默认placeholder
        if (!e.placeholder || e.placeholder == '') {
            e.placeholder = '请输入' + e.label;
        }

        //校验规则处理
        //初始化规则数组
        let eRules = [];
        if (e.type === 'text' || e.type == 'password' || e.type == 'textarea') {
            eRules.push({
                required: e.required,
                validator: validator.stringValidator,
                message: e.label + '值不能为空'
            })
        } else if (e.type == 'url') {
            eRules.push({
                required: e.required,
                validator: validator.stringValidator,
                message: e.label + '值不能为空'
            })
            eRules.push({type: 'url', message: '网址格式不正确'})
        } else if (e.type == 'email') {
            eRules.push({
                required: e.required,
                validator: validator.stringValidator,
                message: e.label + '值不能为空'
            })
            eRules.push({type: 'email', message: '邮箱格式不正确'})
        } else if (e.type == 'tel') {
            eRules.push({
                required: e.required,
                validator: validator.stringValidator,
                message: e.label + '值不能为空'
            })
            eRules.push({message: '手机号格式不正确', validator: validator.mobileValidator})
        } else if (e.type == 'radio') {
            eRules.push({
                required: e.required,
                validator: validator.stringValidator,
                trigger: 'change',
                message: '请选择一项'
            })
        } else if (e.type == 'checkbox') {
            eRules.push({
                required: e.required,
                validator: validator.arrayValidator,
                trigger: 'change',
                message: '请选择一项'
            })
        } else if (e.type == 'date') {
            eRules.push({
                required: e.required,
                validator: validator.dateValidator,
                message: e.label + '值不能为空'
            })
        } else if (e.type == 'dateRange') {
            eRules.push({
                required: e.required,
                validator: validator.dateRangeValidator,
                message: e.label + '值不能为空'
            })
        } else if (e.type == 'datetime') {
            eRules.push({
                required: e.required,
                validator: validator.dateValidator,
                trigger: 'change',
                message: e.label + '值不能为空'
            })
        } else if (e.type == 'datetimeRange') {
            eRules.push({
                required: e.required,
                validator: validator.dateRangeValidator,
                message: e.label + '值不能为空'
            })
        } else if (e.type == 'time') {
            eRules.push({
                required: e.required,
                validator: validator.dateValidator,
                trigger: 'change',
                message: e.label + '值不能为空'
            })
        } else if (e.type == 'timeRange') {
            eRules.push({
                required: e.required,
                validator: validator.dateRangeValidator,
                trigger: 'change',
                message: e.label + '值不能为空'
            })
        } else if (e.type == 'singleSelect') {
            eRules.push({
                required: e.required,
                validator: validator.stringValidator,
                message: '请至少选择一项'
            })
        } else if (e.type == 'multipleSelect') {
            eRules.push({
                required: e.required,
                validator: validator.arrayValidator,
                message: '请至少选择一项'
            })
        } else if (e.type == 'number') {
            eRules.push({
                required: e.required,
                validator: validator.stringValidator,
                message: e.label + '值不能为空'
            })
        } else if (e.type == 'currency') {
            eRules.push({
                required: e.required,
                validator: validator.stringValidator,
                message: e.label + '值不能为空'
            })
        }

        //设置格式样式
        if (!e.format) {
            if (e.type == 'date' || e.type == 'dateRange') {
                e.format = 'yyyy-MM-dd';
            } else if (e.type == 'datetime' || e.type == 'datetimeRange') {
                e.format = 'yyyy-MM-dd HH:mm:ss';
            } else if (e.type == 'time' || e.type == 'timeRange') {
                e.format = 'HH:mm:ss';
            }
        }

        grid[e.prop] = eRules;
    });
};
